package com.apestech.framework.db;

import java.sql.Driver;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.sql.Connection;

import org.apache.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;
import com.apestech.framework.util.StringUtil;

/**
 * 数据连接类，支持多种不同类型的数据连接，目前暂时只封装了DruidDataSource，其它如果有新的连接，请在此补充完成
 * @author hupo
 *
 */
public class SqlDataSource extends StringUtil {

	private static final Logger logger = Logger.getLogger(SqlDataSource.class);

	private DruidDataSource druidDs=null;
	private javax.sql.DataSource sqlDs=null;
	private String classname;
	private String url;
	private String username;
	private String schema;
	private String dbType;
	private Driver driver=null;
	private String id;

	public SqlDataSource(Object datasource) throws SQLException{
		if(datasource==null){
			throw new SQLException(getMessage("SqlDataSource_isNull","datasource不允许为空值 "));
		}
		this.classname=datasource.getClass().getName();
		if(datasource instanceof DruidDataSource){
			this.druidDs=(DruidDataSource) datasource;
			iniDruid();
		}
		iniDbType();
		iniDBInfo();
		setMarkSet();
	}

	public void setMarkSet(){
		if(!hasConnectProp("remarks")){
			Properties properties = new Properties();
			properties.setProperty("remarks", "true");
			properties.setProperty("remarksReporting","true");
			properties.setProperty("useInformationSchema", "true");
			druidDs.setConnectProperties(properties);
		}
	}

	private void iniDruid(){
		url=druidDs.getUrl();
		username=druidDs.getUsername().toUpperCase();
	}

	private void iniDbType(){
		String surl=url.toUpperCase();
		if(surl.indexOf("ORACLE")>=0){
			dbType="ORACLE";
		}
		int pos=surl.indexOf("@");
		if(pos>=0){
			id=surl.substring(pos, surl.length());
			id=username+id.replace(".","_");
		}
	}

	private void iniDBInfo(){
		if("ORACLE".equals(dbType)){
			schema=username;
		}
	}

	public String getDbType(){
		return dbType;
	}

	public Connection getConnection() throws SQLException{
		if("com.alibaba.druid.pool.DruidDataSource".equals(classname)){
			return druidDs.getConnection();
		}
		return null;
	}

	public String getUrl(){
		return url;
	}

	public String getSchema(){
		return schema;
	}

	public String getUsername(){
		return username;
	}

	public String getId() {
		return id;
	}

	public boolean hasConnectProp(String propname){
		Properties properties=null;
		if("com.alibaba.druid.pool.DruidDataSource".equals(classname)){
			properties=druidDs.getConnectProperties();
		}
		if(properties==null){
			return false;
		} else {
			return (properties.getProperty(propname)!=null);
		}
	}

	public void close(){
		if(druidDs!=null){
			druidDs.close();
		}
	}

	public int getActiveCount(){
		if(druidDs!=null){
			druidDs.getActiveCount();
		}
		return 0;
	}

	public List getActiveConnectionStackTrace(){
		if(druidDs!=null){
			return druidDs.getActiveConnectionStackTrace();
		}
		return null;
	}

	public DruidDataSource getDruidDataSource(){
		return druidDs;
	}

}
